/**
 * 
 */
package com.zxc.redis.RLock;

import java.util.concurrent.TimeUnit;

import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

/**
 * @author Administrator
 *
 */
public class RLockDemo {

	public static void main(String[] args) throws InterruptedException {
		Config config = new Config();
		config
			//.setCodec(new FstCodec())
			.useSingleServer() //单例模式
			.setAddress("192.168.1.122:6379")//连接地址
			.setDatabase(1) // 数据库索引位置
			.setPassword("shijie99test") // 密码
			.setClientName("channelbatch") // 客户端名称
			;
		RedissonClient redisson = Redisson.create(config);
		
		RLock lock = redisson.getLock("anyLock");
		
		// Most familiar locking method
		lock.lock();
		
		System.out.println("当前锁是否已被锁住："+lock.isLocked());
		
		// Lock time-to-live support
		// releases lock automatically after 10 seconds
		// if unlock method not invoked
		lock.lock(10, TimeUnit.SECONDS);
		System.out.println("当前锁是否已被锁住："+lock.isLocked());
		
		Thread.sleep(10*1000);
		System.out.println("当前锁是否已被锁住："+lock.isLocked());
		
		// Wait for 100 seconds and automatically unlock it after 10 seconds
		boolean res = lock.tryLock(100, 10, TimeUnit.SECONDS);
		System.out.println("尝试调用锁后的结果："+res);
		lock.unlock();
		
		redisson.shutdown();
	}
}
